/*
 * File Name:               ConfigSettingEditor.cs
 * 
 * Description:             普通类
 * Author:                  lisiyu <576603306@qq.com>
 * Create Date:             2017/01/09
 */

using System;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class XLocalSettingEditorWindow : EditorWindow
{
    [MenuItem("Tools/本地数据配置")]
    private static void onClick()
    {
        GetWindow<XLocalSettingEditorWindow>("本地数据配置").Show();
    }

    private Vector2 scrollPos;
    private static IXBaseLocalSetting mLocalSetting;
    private static Type[] mTypes;
    
    private void addButton(ref int index, float width)
    {
        if (index >= mTypes.Length)
            return;

        var type = mTypes[index++];

        var name = type.Name;
        var attributes = type.GetCustomAttributes(typeof(NickNameAttribute), true);
        if (attributes != null && attributes.Length > 0)
        {
            name = (attributes[0] as NickNameAttribute).NickName;
        }

        if(GUILayout.Button(name, GUILayout.Width(width)))
        {
            save();
            mLocalSetting = type.BaseType.GetProperty("Singleton").GetValue(null, null) as IXBaseLocalSetting;
        }
    }

    void OnGUI()
    {
        if (mTypes == null)
            mTypes = FindAllClass(typeof(IXBaseLocalSetting));

        int i = 0;
        var width = this.position.width / 5 - 5;
        while(i < mTypes.Length)
        {
            EditorGUILayout.BeginHorizontal();
            addButton(ref i, width);
            addButton(ref i, width);
            addButton(ref i, width);
            addButton(ref i, width);
            addButton(ref i, width);
            EditorGUILayout.EndHorizontal();
        }


        if (mLocalSetting == null) return;

        scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
        var tmp = mLocalSetting.GetType();
        string name = tmp.Name;
        var attributes = tmp.GetCustomAttributes(typeof(NickNameAttribute), true);
        if (attributes != null && attributes.Length > 0)
        {
            name = (attributes[0] as NickNameAttribute).NickName;
        }

        try
        {
            mLocalSetting.display(name, displayProperties: false);
        }
        catch(Exception ex)
        {
            Debug.LogError(ex.Message);
        }
        EditorGUILayout.EndScrollView();
        EditorGUILayout.Space();
    }

    private void save()
    {
        if (mLocalSetting == null) return;
        mLocalSetting.save();
    }

    void OnFocus()
    {
        save();
        AssetDatabase.Refresh();
    }

    void OnLostFocus()
    {
        save();
    }

    //这里找出了实现IOutputArray接口的所有类
    private static Type[] FindAllClass(Type type)
    {
        return AppDomain.CurrentDomain.GetAssemblies()
                            .SelectMany(a => a.GetTypes().Where((t) => {
                                if (t.IsAbstract)
                                    return false;
                                if (!t.GetInterfaces().Contains(type))
                                    return false;
                                return true;
                                }))
                            .ToArray();
    }
}
